package com.hice.demo.sbdbra.common.util;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils
{
  public static final String YYYY_MM = "yyyy-MM";
  public static final String YYYY_MM_DD = "yyyy-MM-dd";
  public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
  public static final String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
  public static final String DD_MM_YYYY = "dd-MM-yyyy";
  public static final String MM_DD_YYYY = "MM-dd-yyyy";
  public static final String HH_MM_SS = "HH:mm:ss";
  public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

  public static String getNowTime()
  {
    return getNowTime("yyyy-MM-dd HH:mm:ss");
  }

  public static String getNowTime(String format)
  {
    Date now = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    String dateString = dateFormat.format(now);
    return dateString;
  }

  public static String getToday()
  {
    Calendar calendar = Calendar.getInstance();
    return DateFormat.getDateInstance().format(calendar.getTime());
  }

  public static int getYear()
  {
    return Calendar.getInstance().get(1);
  }

  public static int getMonth()
  {
    return Calendar.getInstance().get(2) + 1;
  }

  public static boolean isLeapYear(int year)
  {
    if (year % 100 == 0) {
      return year % 400 == 0;
    }
    return year % 4 == 0;
  }

  public static int getDayOfYear()
  {
    return Calendar.getInstance().get(6);
  }

  public static int getDayOfMonth()
  {
    return Calendar.getInstance().get(5);
  }

  public static int getDayOfWeek()
  {
    return Calendar.getInstance().get(7);
  }

  public static int getWeekOfMonth()
  {
    return Calendar.getInstance().get(8);
  }

  public static Date getFirstDayOfMonth()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(5, 1);
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    calendar.set(14, 0);

    return calendar.getTime();
  }

  public static Date getLastDayOfMonth()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(5, 1);
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    calendar.set(14, 0);

    calendar.add(2, 1);
    calendar.add(5, -1);

    return calendar.getTime();
  }

  public static Date getFirstDayOfPreviousMonth()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(5, 1);
    calendar.add(2, -1);

    calendar.set(5, 1);
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    calendar.set(14, 0);

    return calendar.getTime();
  }

  public static Date getLastDayOfPreviousMonth()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.set(5, 1);
    calendar.add(5, -1);

    calendar.set(5, 1);
    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    calendar.set(14, 0);

    return calendar.getTime();
  }

  public static String getFirstDayOfNextMonth(String format)
  {
    Calendar lastDate = Calendar.getInstance();
    lastDate.set(5, 1);
    lastDate.add(2, 1);
    return new SimpleDateFormat(format).format(lastDate.getTime());
  }

  public static String getLastDayOfNextMonth(String format)
  {
    Calendar lastDate = Calendar.getInstance();
    lastDate.set(5, 1);
    lastDate.add(2, 2);
    lastDate.add(5, -1);
    return new SimpleDateFormat(format).format(lastDate.getTime());
  }

  public static String getMonthFirst(int num, String format)
  {
    Calendar lastDate = Calendar.getInstance();
    lastDate.set(5, 1);
    lastDate.add(2, num);
    String date = new SimpleDateFormat(format).format(lastDate.getTime());
    return date;
  }

  public static String getMonthEnd(int num, String format)
  {
    Calendar lastDate = Calendar.getInstance();
    lastDate.set(5, 1);
    lastDate.add(2, num + 1);
    lastDate.add(5, -1);
    String date = new SimpleDateFormat(format).format(lastDate.getTime());
    return date;
  }

  public static Date getMondayOfWeek()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(2);
    calendar.set(7, 2);
    calendar.add(4, 0);

    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    calendar.set(14, 0);

    return calendar.getTime();
  }

  public static Date getSundayOfWeek()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(2);
    calendar.set(7, 1);
    calendar.add(4, 0);

    calendar.set(11, 0);
    calendar.set(12, 0);
    calendar.set(13, 0);
    calendar.set(14, 0);

    return calendar.getTime();
  }

  public static String getPreviousMonday()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.add(5, -7);
    calendar.set(7, 2);
    return DateFormat.getDateInstance().format(calendar.getTime());
  }

  public static String getPreviousSunday()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(2);
    calendar.set(7, 1);
    calendar.add(4, -1);
    return DateFormat.getDateInstance().format(calendar.getTime());
  }

  public static String getNextMonday()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.add(5, 7);
    calendar.set(7, 2);
    return DateFormat.getDateInstance().format(calendar.getTime());
  }

  public static String getNextSunday()
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(2);
    calendar.set(7, 1);
    calendar.add(4, 1);
    return DateFormat.getDateInstance().format(calendar.getTime());
  }

  public static String getWeekMonday(int num, String format)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.add(5, num * 7);
    calendar.set(7, 2);
    return new SimpleDateFormat(format).format(calendar.getTime());
  }

  public static String getWeekSunday(int num, String format)
  {
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(2);
    calendar.set(7, 1);
    calendar.add(4, num);
    return new SimpleDateFormat(format).format(calendar.getTime());
  }

  public static String getWeek(String strDate)
  {
    Date date = new SimpleDateFormat("yyyy-MM-dd").parse(strDate, new ParsePosition(0));

    Calendar c = Calendar.getInstance();
    c.setTime(date);

    return new SimpleDateFormat("EEEE").format(c.getTime());
  }

  public static String getWeek(String strDate, String num, String format)
  {
    Date date = getDateParse(strDate, "yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if ("1".equals(num))
      calendar.set(7, 2);
    else if ("2".equals(num))
      calendar.set(7, 3);
    else if ("3".equals(num))
      calendar.set(7, 4);
    else if ("4".equals(num))
      calendar.set(7, 5);
    else if ("5".equals(num))
      calendar.set(7, 6);
    else if ("6".equals(num))
      calendar.set(7, 7);
    else if ("0".equals(num)) {
      calendar.set(7, 1);
    }
    return new SimpleDateFormat(format).format(calendar.getTime());
  }

  public static String addDays(int num, String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Calendar calendar = Calendar.getInstance();
    calendar.add(5, num);
    String str = sdf.format(calendar.getTime());
    return str;
  }

  public static String addHour(int num, String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Calendar calendar = Calendar.getInstance();
    calendar.add(10, num);

    return sdf.format(calendar.getTime());
  }

  public static String addDate(String field, int num)
  {
    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar rightNow = Calendar.getInstance();
    Date dt = new Date();
    rightNow.setTime(dt);

    String tmpField = field.toUpperCase().trim();

    int intField = 5;
    if (tmpField.equals("YEAR"))
      intField = 1;
    else if (tmpField.equals("MONTH"))
      intField = 2;
    else if (tmpField.equals("DAY"))
      intField = 5;
    else if (tmpField.equals("HOUR"))
      intField = 11;
    else if (tmpField.equals("MINUTE"))
      intField = 12;
    else if (tmpField.equals("SECOND")) {
      intField = 13;
    }

    rightNow.add(intField, num);
    String day = dateformat.format(rightNow.getTime());
    return day;
  }

  public static String convertDateToString(Date dateTime, String format)
  {
    SimpleDateFormat df = new SimpleDateFormat(format);
    return df.format(dateTime);
  }

  public static Date getDateParse(String strDate, String format)
  {
    return new SimpleDateFormat(format).parse(strDate, new ParsePosition(0));
  }

  public static long getDiffDate(String startDate, String endDate)
  {
    if ((startDate == null) || (startDate.equals(""))) {
      return 0L;
    }
    if ((endDate == null) || (endDate.equals(""))) {
      return 0L;
    }

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date dateBegin = null;
    Date dateEnd = null;
    try {
      dateBegin = df.parse(startDate);
      dateEnd = df.parse(endDate);
    } catch (Exception e) {
      return 0L;
    }
    long diffDay = (dateBegin.getTime() - dateEnd.getTime()) / 86400000L;
    return diffDay;
  }

  public static long getSpanDays(String startDate, String endDate)
  {
    long spanDay = Math.abs(getDiffDate(startDate, endDate));
    return spanDay;
  }

  public static boolean getTimeStepSize(String strDate1, String strDate2, String format)
  {
    Date date1 = getDateParse(strDate1, format);
    Date date2 = getDateParse(strDate2, format);
    long time1 = date1.getTime();
    long time2 = date2.getTime();
    return time1 > time2;
  }

  public static void main(String[] args)
  {
    System.out.println("获取当天日期:" + getNowTime("yyyy-MM-dd HH:mm:ss"));
    System.out.println("获取今天日期:" + getToday());
    System.out.println("获取当前年份：" + getYear());
    System.out.println("获取当前月份：" + getMonth());
    System.out.println("获取今天在本年的第几天：" + getDayOfYear());
    System.out.println("获得今天在本月的第几天(获得当前日)：" + getDayOfMonth());
    System.out.println("获得今天在本周的第几天：" + getDayOfWeek());
    System.out.println("获取当前月的第几周：" + getWeekOfMonth());
    System.out.println("获取下月第一天日期:" + getFirstDayOfNextMonth("yyyy-MM-dd"));
    System.out.println("获取下月最后一天日期:" + getLastDayOfNextMonth("yyyy-MM-dd"));
    System.out.println("获取某月第一天日期:" + getMonthFirst(0, "yyyy-MM-dd"));
    System.out.println("获取某月最后一天的日期:" + getMonthEnd(0, "yyyy-MM-dd"));
    System.out.println("获取上周一的日期:" + getPreviousMonday());
    System.out.println("获取上周日的日期:" + getPreviousSunday());
    System.out.println("获取下周一的日期:" + getNextMonday());
    System.out.println("获取下周日的日期:" + getNextSunday());
    System.out.println("获取上周一的日期:" + getWeekMonday(-1, "yyyy-MM-dd"));
    System.out.println("获取上周日的日期:" + getWeekSunday(-1, "yyyy-MM-dd"));
    System.out.println("2012-10-25是星期几：" + getWeek("2012-10-25"));
    System.out.println("获得一个日期所在周的星期几的日期:" + getWeek("2012-10-25", "0", "yyyy-MM-dd HH:mm:ss"));
    System.out.println("添加日期天数:" + addDays(2, "yyyy-MM-dd HH:mm:ss"));
    System.out.println("获得新日期:" + addDate("month", -2));
    System.out.println("将日期转换成字符串:" + convertDateToString(new Date(), "yyyy-MM-dd"));
    System.out.println("字符串转时间:" + getDateParse("2012-10-25", "yyyy-MM-dd"));
    System.out.println("两个时间相差的天数2012-10-25~2012-10-27: " + getDiffDate("2012-10-25", "2012-10-27"));
    System.out.println("获取两个日期间隔天数2012-10-25~2012-10-27: " + getSpanDays("2012-10-25", "2012-10-27"));
    System.out.println(getTimeStepSize("2012-10-25 12:25:50", "2012-10-25 13:00:50", "yyyy-MM-dd HH:mm:ss"));
  }
}